/*
 * Copyright (c) 2006-2008 Reliable Source, Inc and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Mozilla Public License v.1.1
 * and BSD licenses.
 * 
 * http://www.eclipse.org/legal/epl-v10.html
 * http://www.opensource.org/licenses/bsd-license.php
 * http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * Contributors:
 *      IBM Corporation - initial API and implementation
 *      patientos.org - domain and database persistence layer              
 *      Reliable Source, Inc. - user interface client platform
 *
 */
package org.reliablesource.symbiosys.workbench.tests.mockobjects;

import org.eclipse.core.runtime.ListenerList;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.PartInitException;
import org.reliablesource.client.ui.viewers.IReliableViewer;
import org.reliablesource.client.ui.workbench.views.ReliableViewPart;
import org.reliablesource.symbiosys.ui.tests.viewers.MockSelectorViewer;

/**
 * 
 * SelectionProviderView.java<br>
 * Created on: July 17, 2006<p>
 *
 * @author <a href="mailto:dennis.park@gmail.com">Dennis Park</a>
 *
 * @category
 * @since 1.0
 */
public class SelectionProviderView extends ReliableViewPart implements
	ISelectionProvider {
    public final static String ID = "org.reliablesource.symbiosys.workbench.tests.mockobjects.views.selectionprovider";
    public final static String ID_2 = "org.reliablesource.symbiosys.workbench.tests.mockobjects.views.selectionprovider2";

    private ListenerList selectionChangedListeners;
    protected ISelection currentSelection;

    @Override
    public void init(IViewSite _site) throws PartInitException {
	super.init(_site);
	selectionChangedListeners = new ListenerList();
	_site.setSelectionProvider(this);
    }

    /**
     * @see ISelectionProvider#addSelectionChangedListener(ISelectionChangedListener)
     */
    public void addSelectionChangedListener(ISelectionChangedListener listener) {
	selectionChangedListeners.add(listener);
    }

    /**
     * @see ISelectionProvider#getSelection()
     */
    public ISelection getSelection() {
	return currentSelection;
    }

    /**
     * @see ISelectionProvider#removeSelectionChangedListener(ISelectionChangedListener)
     */
    public void removeSelectionChangedListener(
	    ISelectionChangedListener listener) {
	selectionChangedListeners.remove(listener);
    }

    /**
     * @see ISelectionProvider#setSelection(ISelection)
     */
    public void setSelection(ISelection _selection) {
	if (null == _selection)
	    _selection = StructuredSelection.EMPTY;

	currentSelection = _selection;
	getSite().getSelectionProvider().setSelection(currentSelection);
	notifyListeners();
    }

    private void notifyListeners() {
	// create an event
	SelectionChangedEvent _event = new SelectionChangedEvent(this,
		currentSelection);

	// fire the event
	Object[] _listeners = selectionChangedListeners.getListeners();
	for (int i = 0; i < _listeners.length; ++i)
	    ((ISelectionChangedListener) _listeners[i])
		    .selectionChanged(_event);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.reliablesource.client.ui.workbench.views.ReliableViewPart#createViewer()
     */
    @Override
    protected IReliableViewer createViewer() {
	return new MockSelectorViewer();
    }

    /* (non-Javadoc)
     * @see org.reliablesource.client.ui.workbench.views.IReliableViewPart#getPartId()
     */
    public String getPartId() {
	return ID;
    }
}
